package cn.ssms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ssms.model.Log;
import cn.ssms.service.CommonService;
import cn.ssms.service.UserService;
import cn.ssms.util.DataAccessException;
import cn.ssms.util.GetRequestParam;
import cn.ssms.util.LogUtil;
import cn.ssms.util.MyUtils;
import cn.ssms.util.SystemConstant;

import com.google.gson.Gson;

@Controller
public class UserControler {

	@Autowired
	private UserService userService;
	@Autowired
	private CommonService commonService;
	
	/**
	 * 一、获取令牌接口
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getToken", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)  
	@ResponseBody  
	public String getToken(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"TOKEN管理",
				"获取TOKEN","tel:"+map.get("tel")+""+"<br/>password:"+"不记录", null, null);	
		try {
			result =  userService.getToken(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");		
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","获取TOKEN失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("获取TOKEN失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 二、注册
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/regist", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)  
	@ResponseBody  
	public String regist(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"用户注册","tel:"+map.get("tel")+"<br/>telcode:"+map.get("telcode")+"<br/>password:"+"不记录", null, null);		
		try {
			result =  userService.regist(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","注册失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("注册失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	/**
	 * 三、判断电话号码是否已注册
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/telIsRegisted", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)  
	@ResponseBody  
	public String telIsRegisted(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"手机号是否存在","tel:"+map.get("tel")+"", null, null);
		try {
			result =  userService.telIsRegisted(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");		
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","手机号判断失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("手机号判断失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 四、获取短信验证码
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/sendsms", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)  
	@ResponseBody  
	public String sendsms(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"获取短信验证码","tel:"+map.get("tel")+""+"type:"+map.get("type")+"", null, null);
		try {
			result =  userService.sendsms(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");		
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","获取短信验证码失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("获取短信验证码失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	/**
	 * 五、获取个人信息
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getUserMessage", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)  
	@ResponseBody  
	public String getUserMessage(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"获取用户信息","tokenid:"+map.get("tokenid")+""+"userid:"+map.get("userid")+"", null, null);		
		try {
			result =  userService.getUserMessage(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","获取个人信息失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("获取个人信息失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 六、修改密码
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/updatePassword", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)  
	@ResponseBody  
	public String updatePassword(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"修改密码","tokenid:"+map.get("tokenid")+"<br/>password:"+"不记录", null, null);		
		try {
			result =  userService.updatePassword(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","修改密码失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("修改密码失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 七、修改名字
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/updateName", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)  
	@ResponseBody  
	public String updateName(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"修改名字","tokenid:"+map.get("tokenid")+"<br/>name:"+map.get("name"), null, null);		
		try {
			result =  userService.updateName(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","修改名字失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("修改名字失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 八、修改个性签名
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/updateSign", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)  
	@ResponseBody  
	public String updateSign(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"修改个性签名","tokenid:"+map.get("tokenid")+"<br/>signature:"+map.get("signature"), null, null);		
		try {
			result =  userService.updateSign(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","修改个性签名失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("修改个性签名失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 九、修改性别
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/updateSex", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)  
	@ResponseBody  
	public String updateSex(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"修改性别","tokenid:"+map.get("tokenid")+"<br/>sex:"+map.get("sex"), null, null);		
		try {
			result =  userService.updateSex(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","修改性别失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("修改性别失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 十、上传头像
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/updateIcon", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)  
	@ResponseBody  
	public String updateIcon(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"上传头像","tokenid:"+map.get("tokenid")+"<br/>icon:不记录", null, null);		
		try {
			map.put("path", request.getSession().getServletContext().getRealPath("/"));
			result =  userService.updateIcon(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","上传头像失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("上传头像失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 十一、获取积分卡信息
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getCards", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)  
	@ResponseBody  
	public String getCards(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"获取积分卡信息","", null, null);		
		try {
			result =  userService.getCards(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","获取积分卡信息失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("获取积分卡信息失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 十二、登录
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/applogin", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)  
	@ResponseBody  
	public String applogin(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"登录","tel:"+map.get("tel")+"<br/>password:"+map.get("password"), null, null);		
		try {			
			result =  userService.applogin(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","登录失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("登录失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 十三、获取门店下拉框
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/storeComboBox", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)  
	@ResponseBody  
	public String storeComboBox(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"获取门店下拉框","", null, null);		
		try {
			result =  userService.storeComboBox(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","获取门店下拉框失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("获取门店下拉框失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	/**
	 * 十四、忘记密码
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/forgetPassword", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)  
	@ResponseBody  
	public String forgetPassword(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"忘记密码","tel:"+map.get("tel")+"<br/>telcode:"+map.get("telcode")+"<br/>password:"+"不记录", null, null);		
		try {
			result =  userService.forgetPassword(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","找回密码失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("找回密码失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	

	/**
	 * 十五、根据门店信息和日期查询可预约课程（分页）
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/getListCourse", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
	@ResponseBody
	public String getListCourse(HttpServletRequest request, HttpServletResponse response, Model model){		
		Integer page = request.getParameter("page")==null?1:Integer.valueOf(request.getParameter("page"));
		Integer pageSize = request.getParameter("pageSize")==null?SystemConstant.PAGESIZE:Integer.valueOf(request.getParameter("pageSize"));
		Map<String,Object> map = GetRequestParam.setMap(request);	
		map.put("page", (page-1)*pageSize);
		map.put("pageSize", pageSize);		
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"获取课程信息","tokenid:"+map.get("tokenid")+"<br/>starttime:"+map.get("starttime")
				+"<br/>storeid:"+map.get("storeid")+"<br/>type:"+map.get("type")+"<br/>page:"+page+"<br/>pageSize:"+pageSize, null, null);		
		try {		
			result =  userService.getListCourse(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","根据门店信息和日期查询可预约课程失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("根据门店信息和日期查询可预约课程失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 十六、购买会员卡
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/buyCard", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)  
	@ResponseBody  
	public String buyCard(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"购买会员卡","tel:"+map.get("tel")+"<br/>cardid:"+map.get("cardid")+"<br/>tokenid:"+map.get("tokenid"), null, null);		
		try {
			result =  userService.buyCard(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			e.getMessage();
			result.put("code", 500);
			result.put("message","购买会员卡失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("购买会员卡失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 十七、获取购卡记录信息
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/getCardRecord", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
	@ResponseBody
	public String getCardRecord(HttpServletRequest request, HttpServletResponse response, Model model){		
		Map<String,Object> map = GetRequestParam.setMap(request);	
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"获取购卡记录信息","tokenid:"+map.get("tokenid"), null, null);		
		try {		
			result =  userService.getCardRecord(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","获取购卡记录信息失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("获取购卡记录信息失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 十八、预约课程
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/orderCourse", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)  
	@ResponseBody  
	public String orderCourse(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"预约课程","courseid:"+map.get("courseid")+"<br/>tokenid:"+map.get("tokenid"), null, null);		
		try {
			result =  userService.orderCourse(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			e.getMessage();
			result.put("code", 500);
			result.put("message","预约课程失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("预约课程失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 十九、取消预约课程
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/unOrderCourse", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)  
	@ResponseBody  
	public String unOrderCourse(HttpServletRequest request, HttpServletResponse response, Model model){  
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"取消预约课程","courseid:"+map.get("courseid")+"<br/>tokenid:"+map.get("tokenid"), null, null);		
		try {
			result =  userService.unOrderCourse(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			e.getMessage();
			result.put("code", 500);
			result.put("message","取消预约课程失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("取消预约课程失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 二十、获取课程详细信息
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/findCourseMes", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
	@ResponseBody
	public String findCourseMes(HttpServletRequest request, HttpServletResponse response, Model model){		
		Map<String,Object> map = GetRequestParam.setMap(request);	
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"获取课程详细信息","courseid:"+map.get("courseid")+"<br/>tokenid:"+map.get("tokenid"), null, null);		
		try {		
			result =  userService.findCourseMes(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","获取课程详细信息失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("获取课程详细信息失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	/**
	 * 二十一、获取首页图片信息
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/homeHead", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
	@ResponseBody
	public String homeHead(HttpServletRequest request, HttpServletResponse response, Model model){		
		Map<String,Object> map = GetRequestParam.setMap(request);	
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"获取首页图片信息","", null, null);		
		try {		
			result =  userService.homeHead(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","获取首页图片信息失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("获取首页图片信息失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 二十二、获取课程历史记录
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/getCourseRecord", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
	@ResponseBody
	public String getCourseRecord(HttpServletRequest request, HttpServletResponse response, Model model){		
		Map<String,Object> map = GetRequestParam.setMap(request);	
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"获取课程历史记录","tokenid:"+map.get("tokenid"), null, null);		
		try {		
			result =  userService.getCourseRecord(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","获取课程历史记录失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("获取课程历史记录失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 二十三、获取版本信息
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/getVersion", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
	@ResponseBody
	public String getVersion(HttpServletRequest request, HttpServletResponse response, Model model){		
		Map<String,Object> map = GetRequestParam.setMap(request);	
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"获取版本信息","version:"+map.get("version"), null, null);		
		try {		
			result =  userService.getVersion(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","获取版本信息失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("获取版本信息失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 二十四、获取用户列表
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/getOrderedMember", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
	@ResponseBody
	public String getOrderedMember(HttpServletRequest request, HttpServletResponse response, Model model){		
		Map<String,Object> map = GetRequestParam.setMap(request);	
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"获取用户列表","courseid:"+map.get("courseid"), null, null);		
		try {		
			result =  userService.getOrderedMember(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message","获取用户列表失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("获取用户列表失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
	}
	
	/**
	 * 支付接口
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/alipay",produces="text/html;charset=UTF-8",method={RequestMethod.POST})
	public String alipay(HttpServletRequest request, HttpServletResponse response, Model model){	
		Map<String,Object> map = GetRequestParam.setMap(request);			
		Map<String,Object> result =  new HashMap<String, Object>();
		Log log=new Log();
		log = LogUtil.createLog(map.get("tel")+"",MyUtils.getIpAddr(request),"移动端用户管理",
				"购买会员卡","tel:"+map.get("tel")+"<br/>cardid:"+map.get("cardid")+"<br/>tokenid:"+map.get("tokenid"), null, null);		
		try {
			result =  userService.alipay(map);
			log.setState("记录");
			log.setDescribe(result.get("message")+"");			
		}catch (DataAccessException ee) {
			result.put("code",502);
			result.put("message",ee.getMessage());
			log.setState("警告");
			log.setDescribe(ee.getMessage());
		} catch (Exception e) {
			e.getMessage();
			result.put("code", 500);
			result.put("message","购买会员卡失败，请稍后再试");
			log.setState("错误");
			log.setDescribe("购买会员卡失败" + e.getMessage());
		}
		commonService.addLog(log);
		return new Gson().toJson(result);
		/* //公共参数
        Map<String, String> map = new HashMap<String, String>();
        map.put("app_id", AlipayConfig.app_id);
        map.put("method", "alipay.trade.app.pay");
        map.put("format", "json");
        map.put("charset", "utf-8");
        map.put("sign_type", "RSA");
        map.put("timestamp", DateUtil.getNowDate());
        map.put("version", "1.0");
        map.put("notify_url", AlipayConfig.service);

        Map<String, String> m = new HashMap<String, String>();

        m.put("body", "金舞团会员卡购买");
        m.put("subject", "金舞团会员卡");
        m.put("out_trade_no", out_trade_no);
        m.put("timeout_express", "30m");
        m.put("total_amount", total_amount);
        m.put("seller_id", AlipayConfig.partner);
        m.put("product_code", "QUICK_MSECURITY_PAY");

        JSONObject bizcontentJson= JSONObject.fromObject(m);

        map.put("biz_content", bizcontentJson.toString());
       //对未签名原始字符串进行签名       
        String rsaSign = AlipaySignature.rsaSign(map, AlipayConfig.private_key, "utf-8");

        Map<String, String> map4 = new HashMap<String, String>();

        map4.put("app_id", AlipayConfig.app_id);
        map4.put("method", "alipay.trade.app.pay");
        map4.put("format", "json");
        map4.put("charset", "utf-8");
        map4.put("sign_type", "RSA");
        map4.put("timestamp", DateUtil.getNowDate());
        map4.put("version", "1.0");
        map4.put("notify_url",  URLEncoder.encode(AlipayConfig.service,"UTF-8"));
        //最后对请求字符串的所有一级value（biz_content作为一个value）进行encode，编码格式按请求串中的charset为准，没传charset按UTF-8处理
        map4.put("biz_content", URLEncoder.encode(bizcontentJson.toString(), "UTF-8"));
        Map<String, String> par = AlipayCore.paraFilter(map4); //除去数组中的空值和签名参数
        String json4 = AlipayCore.createLinkString(map4);   //拼接后的字符串
        json4=json4 + "&sign=" + URLEncoder.encode(rsaSign, "UTF-8");
        Map<String,Object> result =  new HashMap<String, Object>();
   	    result.put("code", 200);
   	    result.put("message","支付成功");
   	    result.put("data", json4.toString());
   	    return new Gson().toJson(result);*/
	}
	
	/**
	 * 回调接口
	 */
	@ResponseBody
	@RequestMapping(value = "/callbacks",produces = "text/html;charset=UTF-8",method={RequestMethod.POST})
	public String callbacks(HttpServletRequest request, HttpServletResponse response, Model model){	
	        //接收支付宝返回的请求参数
	        Map<String, String[]> requestParams = request.getParameterMap();
	        JSONObject json = JSONObject.fromObject(requestParams);
	        String trade_status = json.get("trade_status").toString().substring(2,json.get("trade_status").toString().length()-2);
	        String out_trade_no = json.get("out_trade_no").toString().substring(2,json.get("out_trade_no").toString().length()-2);
	        String notify_id = json.get("notify_id").toString().substring(2,json.get("notify_id").toString().length()-2);
	        System.out.println(json.toString());
	        System.out.println("支付宝回调地址！");
	        System.out.println("商户的订单编号：" + out_trade_no);
	        System.out.println("其他参数：" + notify_id);
	        System.out.println("支付的状态：" + trade_status);    
	        if(trade_status.equals("TRADE_SUCCESS")) {
	            //支付成功之后的业务处理	  
	        	try {
					userService.updateCardStatus(out_trade_no);
				} catch (DataAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }else {
	        	//支付失败后的业务处理	  
	        	 System.out.println("支付失败！");
	        }
			/*String out_trade_no = request.getParameter("out_trade_no");
			try {
				userService.updateCardStatus(out_trade_no);
			} catch (DataAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
	        return "SUCCESS";
	}
}
